package com.sd.repay.utils;

import com.sd.repay.common.Constants;
import okhttp3.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

/**
 * OkHttp post请求
 */
public class HttpUtil {

    public static final MediaType JSON = MediaType.parse("application/json; charset=utf-8");
    private static Logger log = LoggerFactory.getLogger(HttpUtil.class);

    public static Response sendPost(String url, String jsonStr) {
        log.info("接口请求参数：" + jsonStr);
        log.info("接口请求地址：" + url);
        OkHttpClient client = new OkHttpClient();
        client.newBuilder().
                connectTimeout(Constants.CONNECT_TIMEOUT, TimeUnit.SECONDS).
                writeTimeout(Constants.WRITE_TIMEOUT, TimeUnit.SECONDS)
                .readTimeout(Constants.READ_TIMEOUT, TimeUnit.SECONDS);
        RequestBody body = RequestBody.create(JSON, jsonStr);
        Request request = new Request.Builder().url(url).post(body).build();
        Response response = null;
        try {
            response = client.newCall(request).execute();
        } catch (IOException e) {
            e.printStackTrace();
            log.info("url：" + url + "接口请求异常：" + e);
        }
        return response;
    }
}
